package com.eagle.roomsearch.study.java8study;

import com.eagle.roomsearch.util.DateUtil;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;

/**
 * @author 林良炫
 * @Description: ${todo}
 * @date 2018/3/2717:22
 */
public class Java8TimeStudy {

    //获取两个时间段的工作日天数（默认周末不上班）,以后定制化再搞，
    private static int getWorkDays(String start, String end, String format, int days) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        LocalDate startDate = LocalDate.parse(start, formatter);
        LocalDate endDate = LocalDate.parse(end, formatter);
        while (startDate.compareTo(endDate) <= 0) {
            //星期六、日设为非必要提交
            int value = DayOfWeek.from(startDate).getValue();
            if (value == 6 || value == 7) {
                days--;
            }
            startDate = startDate.plusDays(1);
        }
        return days;
    }

    //获取本时间段的天数
    private static int getDays(String start, String end, String format) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        LocalDate startDate = LocalDate.parse(start, formatter);
        LocalDate endDate = LocalDate.parse(end, formatter);
        return Long.valueOf(ChronoUnit.DAYS.between(startDate, endDate) + 1).intValue();
    }

    //1检查是否是节假日  --   星期六、日设为非必要提交
    private Boolean isWeekendHoliday(Integer value) {
        if (value == 6 || value == 7) {
            return true;
        }
        return false;
    }

    //获取本季度第一天
    private static String getFirstDayOfThisSeason() {
        LocalDate today = LocalDate.now();
        int year = today.getYear();
        System.out.println("year==" + year);
        String firstDayOfThisSeason = null;
        int month = today.getMonthValue();
        System.out.println("month==" + month);
        if (month >= 1 && month <= 3) {
            firstDayOfThisSeason = "01月01日";
        } else if (month >= 4 && month <= 6) {
            firstDayOfThisSeason = "04月01日";
        } else if (month >= 7 && month <= 9) {
            firstDayOfThisSeason = "07月01日";
        } else {
            firstDayOfThisSeason = "10月01日";
        }
        return year + "年" + firstDayOfThisSeason;
    }


    public static void main(String[] args) {
        BigDecimal a;
        BigDecimal b;
        a = new BigDecimal(1);
        BigDecimal a2 = new BigDecimal(11);
        BigDecimal a3 = new BigDecimal(12);
        b = new BigDecimal(23);
        System.out.println(a.divide(b, 6, RoundingMode.HALF_UP));
        System.out.println(a2.divide(b, 6, RoundingMode.HALF_UP));
        System.out.println(a3.divide(b, 6, RoundingMode.HALF_UP));
        String timeFormatterStr = "yyyy年MM月dd日";
        LocalDate today = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(timeFormatterStr);
        String todayStr = today.format(formatter);
        //今天
        System.out.println("todayStr====" + todayStr);
        //本周第一天
        String firstDayOfThisWeek = DateUtil.getFirstOfWeek(todayStr, timeFormatterStr);
        System.out.println("firstDayOfThisWeek====" + firstDayOfThisWeek);
        //本月第一天
        LocalDate firstDayOfThisMonth = today.with(TemporalAdjusters.firstDayOfMonth());
        System.out.println("firstDayOfThisMonth=====" + firstDayOfThisMonth.format(formatter));
        //本季度第一天
        System.out.println(getFirstDayOfThisSeason());

        int thisWeekUtilToday = getDays(firstDayOfThisWeek, todayStr, timeFormatterStr);

        int thisWeekUtilTodayWorkDay = getWorkDays(firstDayOfThisWeek, todayStr, timeFormatterStr, thisWeekUtilToday);

        System.out.println("thisWeekUtilTodayWorkDay===" + thisWeekUtilTodayWorkDay);

        System.out.println("thisWeekUtilToday===" + thisWeekUtilToday);

        int thisMonthUtilToday = getDays(firstDayOfThisMonth.format(formatter), todayStr, timeFormatterStr);

        int thisMonthUtilTodayWorkDay = getWorkDays(firstDayOfThisMonth.format(formatter), todayStr, timeFormatterStr, thisMonthUtilToday);

        System.out.println("thisMonthUtilTodayWorkDay===" + thisMonthUtilTodayWorkDay);

        System.out.println("thisMonthUtilToday===" + thisMonthUtilToday);

        int thisSeasonUtilToday = getDays(getFirstDayOfThisSeason(), todayStr, timeFormatterStr);
        int thisSeasonUtilTodayWorkDay = getWorkDays(getFirstDayOfThisSeason(), todayStr, timeFormatterStr, thisSeasonUtilToday);
        System.out.println("thisSeasonUtilTodayWorkDay===" + thisSeasonUtilTodayWorkDay);
        System.out.println("thisSeasonUtilToday===" + thisSeasonUtilToday);
    }
}
